using System;
using System.Collections.Generic;
using System.Text;
using MindFactorial.LaunchPoint.Common;

namespace MindFactorial.LaunchPoint.Runtime
{
    /// <summary>
    /// Represents the Business Rule in the LaunchPoint Server
    /// </summary>
    public class BRERule : RuntimeObject, INamedObject<string>
    {        
        string _name = null;
        string _adaptername = null;
        Guid _session_id = default(Guid);
        ///// <summary>
        ///// Fires when rule processing has completed.  This event is obsolete and should not be used
        ///// </summary>
        //[Obsolete()]        
        //public event RuleCompletedEventHandler completed = null;
        LaunchPointApplication _runnable;

        internal BRERule(string adapterid, string ruleid, LaunchPointApplication runnable)
        {             
            this._name = ruleid;
            this._adaptername = adapterid;
            this._runnable = runnable;
            this._session_id = Guid.NewGuid();
            //completed = null;
        }

        /// <summary>
        /// gets the name of the rule
        /// </summary>
        public string Name
        {
            get { return this._name; }
        }

        /// <summary>
        /// Explicitly sets the session id of the rule
        /// </summary>
        /// <param name="session_id"></param>
        internal void SetSession(Guid session_id)
        {
            this._session_id = session_id;
        }

        //TODO: make all EXecuteRule functions take a new type that is more robust in determining
        //whether an error has occured

        #region Synchronous Call
        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="arguments">Argument to pass to the rule, this MUST be in xml format</param>
        public string Execute(string arguments)
        {
            string response = _runnable.BRERequest(_session_id, "default", _adaptername, _name, arguments);
            return response;
        }


        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="channel">the transport provider (bre connection provider) used to send the Message to the 
        /// BRE layer</param>
        /// <param name="arguments">Argument to pass to the rule, this MUST be in xml format</param>
        /// <returns></returns>
        public string Execute(string channel, string arguments)
        {
            string response = _runnable.BRERequest(_session_id, channel, _adaptername, _name, arguments);
            return response;
        }

        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="argument">Argument to pass to the rule, this MUST be in xml format</param>
        public string Execute(IDataEntity argument)
        {
            string response = _runnable.BRERequest(_session_id,"default", _adaptername, _name, argument.Serialize().Value);
            return response;
        }


        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="channel">the transport provider (bre connection provider) used to send the Message to the 
        /// BRE layer</param>
        /// <param name="argument">Argument to pass to the rule, this MUST be in xml format</param>
        /// <returns></returns>
        public string Execute(string channel, IDataEntity argument)
        {
            string response = _runnable.BRERequest(_session_id,channel, _adaptername, _name, argument.Serialize().Value);
            return response;
        } 
        #endregion

        #region AsycnchronousCall
        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="arguments">Argument to pass to the rule, this MUST be in xml format</param>
        /// <param name="acb"></param>
        public void ExecuteAsync(string arguments, AsyncRuleCompletedDelegate acb)
        {
             _runnable.BRERequest(_session_id,"default", _adaptername, _name, arguments, acb);
           
        }


        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="channel">the transport provider (bre connection provider) used to send the Message to the 
        /// BRE layer</param>
        /// <param name="arguments">Argument to pass to the rule, this MUST be in xml format</param>
        /// <param name="acb"></param>
        /// <returns></returns>
        public void ExecuteAsync(string channel, string arguments, AsyncRuleCompletedDelegate acb)
        {
            _runnable.BRERequest(_session_id, channel, _adaptername, _name, arguments, acb);           
        }

        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="argument">Argument to pass to the rule, this MUST be in xml format</param>
        /// <param name="acb"></param>
        public void ExecuteAsync(IDataEntity argument, AsyncRuleCompletedDelegate acb)
        {
            _runnable.BRERequest(_session_id, "default", _adaptername, _name, argument.Serialize().Value, acb);
            
        }


        /// <summary>
        /// Executes the specified rule in the context of the LaunchPoint Server
        /// </summary>
        /// <param name="channel">the transport provider (bre connection provider) used to send the Message to the 
        /// BRE layer</param>
        /// <param name="argument">Argument to pass to the rule, this MUST be in xml format</param>
        /// <param name="acb"></param>
        /// <returns></returns>
        public void ExecuteAsync(string channel, IDataEntity argument, AsyncRuleCompletedDelegate acb)
        {
            _runnable.BRERequest(_session_id, channel, _adaptername, _name, argument.Serialize().Value, acb);
           
        }
        #endregion



        

        

        
    }
}
